var form, $;
layui.extend({
    baseConfig : "/js/pages/baseConfig"
})
layui.use(['form','layer','upload','baseConfig'],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        baseConfig = layui.baseConfig,
        upload = layui.upload;

    //上传头像
    upload.render({
        elem: '.headImgBtn',
        url: '/file/upload?type=headImg',
        method : "post",
        done: function( res){
            if( res.code == 200){
                var filePath = res.data.filePath;
                $('#headImg').attr( 'src', filePath);
                $("input[name=headImg]").val( filePath);
            }
        }
    });

    //设置编辑表单里面的用户信息
    var userInfo = JSON.parse( window.sessionStorage.getItem( "userInfo"));

    $("input[name=id]").val( userInfo.id);
    $("input[name=jobNumber]").val( userInfo.jobNumber);
    $("input[name=name]").val( userInfo.name);
    $("input[name=roleName]").val( userInfo.role.roleName);
    $("input[name=number]").val( userInfo.number);
    $("input[name=phone]").val( userInfo.phone);
    $("input[name=email]").val( userInfo.email);
    $("input[name=headImg]").val( userInfo.headImg);
    $("input[name=nom]").val( userInfo.nom);
    $("input[name=prenom]").val( userInfo.prenom);
    $("#headImg").attr( "src", userInfo.headImg);

    //提交个人资料
    form.on("submit(changeUser)",function(data){
        var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
        $.post("/adminUser/updateUserInfo",{
            'id': data.field.id,
            'jobNumber': data.field.jobNumber,
            'name': data.field.name,
            'phone': data.field.phone,
            'email': data.field.email,
            'headImg': data.field.headImg,
            'nom': data.field.nom,
            'prenom': data.field.prenom
        },function( res){
            layer.close( index);
            if( res.code == 200){
                top.layer.msg( res.msg);
            }if( res.code == 60001){
                top.layer.msg( res.msg);
            }else{
                top.layer.msg( res.msg);
            }
        });
        return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
    })
})
